package com.example.healthapp.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.healthapp.R;
import com.example.healthapp.utils.InterfaceTool;

import java.util.Locale;

// 睡眠页面的Activity
public class SleepActivity extends AppCompatActivity {

    TimePicker sleep_timerpicker;
    TextView sleep_btn, sleep_count;
    Handler handler;
    Thread thread;
    private TextToSpeech textToSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sleep);
        InterfaceTool.setFullScreen(this, false);
        initview();
        event();
    }

    private void event() {
        sleep_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sleep_btn.setEnabled(false);
                int hour = sleep_timerpicker.getCurrentHour();
                int minute = sleep_timerpicker.getCurrentMinute();
                Toast.makeText(SleepActivity.this, hour + "---" + minute, Toast.LENGTH_SHORT).show();

                thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        long totalMillis = (long) hour * 60 * 60 + (long) minute * 60; // 总秒数
                        long millisLeft = totalMillis;  //总秒数
                        while (millisLeft > 0) {
                            millisLeft--;
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            long finalMillisLeft = millisLeft;
                            long hours = millisLeft / (60 * 60);
                            long minutes = (millisLeft % (60 * 60)) / (60);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    sleep_count.setText(hours + "时" + minutes + "分钟" + finalMillisLeft % 60 + "秒");
                                }
                            });
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                sleep_btn.setEnabled(true);
                                textToSpeech.speak("时间到了！时间到了！", TextToSpeech.QUEUE_FLUSH, null, null);
                            }
                        });
                    }
                });
                thread.start();
            }
        });
    }

    private void initview() {
        sleep_timerpicker = findViewById(R.id.sleep_timerpicker);
        sleep_btn = findViewById(R.id.sleep_btn);
        sleep_count = findViewById(R.id.sleep_count);
        handler = new Handler();
        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    // 设置语音引擎的语言
                    Locale locale = Locale.getDefault();
                    int result = textToSpeech.setLanguage(locale);
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "不支持该语言：" + locale);
                    }
                } else {
                    Log.e("TTS", "初始化失败");
                }
            }
        });
    }
}